<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
  
  <head>
    <meta charset="UTF-8">
    <title>查看管理员角色</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" th:href="@{/css/font.css}">
    <link rel="stylesheet" th:href="@{/css/xadmin.css}">
    <script type="text/javascript" th:src="@{/lib/rep/jquery-2.1.1/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form action="" method="post" class="layui-form layui-form-pane">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">
                        拥有角色
                    </label>
                    <input type="hidden" name="uname" id="admin" th:value="${aname}" required>
                    <table  class="layui-table layui-input-block">
                        <tbody>
                        </tbody>
                    </table>
                </div>
                <div class="layui-form-item" style="padding-left: 26px">
                <button class="layui-btn" lay-submit="" lay-filter="edit">修改</button>
              </div>
            </form>
        </div>
    </div>
    <script type="text/javascript" th:src="@{/js/commons.js}"></script>
    <script>
        let form;
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
            form = layui.form;
          let layer = layui.layer;

          //监听提交
          form.on('submit(edit)', function(data){
            let ids = [];
            $('input[name="id"]:checked').each(function (index,domEle) {
                ids.push($(this).val());
            });
            data.field.ids = ids;
            //提交修改
            $.ajax({
                url: '/admin/roles/edit',
                type: 'post',
                async: false,
                data: data.field,
                traditional: true,
                success: function (result) {
                    let r_icon = icon(result);
                    layer.alert(result.content, {icon: r_icon},function () {
                        if (result.type === 'success') {
                            //关闭当前frame
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            // xadmin.father_reload();
                        }
                        layer.close(layer.index);
                    });
                }
            });
            return false;
          });
          //anything here to do
          //获取角色信息并勾选当前管理员角色信息
          $.ajax({
                url: '/admin/roles/info',
                type: 'get',
                success: function (result) {
                    if (result.data.length > 0) {
                        table_data(result.data);
                        has_roles();
                        form.render('checkbox');
                    }
                },
                error:function(XMLHttpRequest) {
                    layer.alert('数据处理失败! 错误码:'+XMLHttpRequest.status,{title: '错误信息',icon: 2});
                }
            });
        });
        //渲染表格
        function table_data(list) {
            let html = '<tr><td><div class="layui-input-block">';
            for (let i = 0; i < list.length; i ++){
                if (list[i].state === 1){
                    html += '<input name="id" lay-skin="primary" type="checkbox" value="'+ list[i].roleId + '" title="' + list[i].roleName + '">';
                }
            }
            html += '</div></td></tr>';
            $('.layui-table tbody').html(html);
        }
        //勾选拥有的角色
        function has_roles() {
            let name = $('#admin').val();
            $.ajax({
                url: '/admin/roles/has',
                type: 'post',
                async: false,
                data: {name:name},
                success: function (result) {
                    let rids = result.data;
                    let vals = $('input');
                    if (result.data != null && rids != null && rids.length > 0){
                        for (let i = 0 ; i < rids.length ; i ++){
                            for (let j = 0 ; j < vals.length ; j++){
                                if (vals[j].type == 'checkbox'){
                                    if (vals[j].value == rids[i].roleId){
                                        vals[j].setAttribute('checked','checked');//勾选
                                        form.render('checkbox');//重新渲染
                                    }
                                }
                            }
                        }
                    }
                },
                error:function(XMLHttpRequest) {
                    layer.alert('数据处理失败! 错误码:'+XMLHttpRequest.status,{title: '错误信息',icon: 2});
                }
            });
        }
    </script>
  </body>

</html>